package cc.linker.steplink.crm.controller;

import cc.linker.steplink.crm.commons.model.PcUserInfo;
import cc.linker.steplink.crm.merchanth5.commons.UserInfoUtil;
import cc.linker.steplink.crm.service.IClassificationService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/classification")
public class ClassificationController {

    @Autowired
    private IClassificationService classificationService;
    @Autowired
    private UserInfoUtil userInfoUtil;

    /**
     * 类目列表
     *
     * @param pageIndex
     * @param pageSize
     * @param pid
     */
    @PostMapping("/list")
    public PageInfo listClassification(@RequestBody Map<String, Object> param, HttpServletRequest request) {
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        param.put("orgId", userinfo.getOrgId());
        return classificationService.listClassification(param);
    }

    /**
     * 新增类目
     *
     * @param name 名称
     * @param pid  父级id，新增子类目时传
     */
    @PostMapping("/add")
    public void add(@RequestBody Map<String, Object> param, HttpServletRequest request) {
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        param.put("orgId", userinfo.getOrgId());
        classificationService.insertClassification(param);
    }

    /**
     * 编辑类目
     *
     * @param id   id
     * @param pid  pid
     * @param name 名称
     */
    @PostMapping("/edit")
    public void edit(@RequestBody Map<String, Object> param, HttpServletRequest request) {
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        param.put("orgId", userinfo.getOrgId());
        classificationService.updateClassification(param);
    }

    /**
     * 删除类目
     *
     * @param id id
     */
    @PostMapping("/del")
    public void del(@RequestBody Map<String, Object> param) {
        classificationService.deleteClassification(param);
    }

    /**
     * 类目列表，不分页
     *
     * @param pid pid
     */
    @PostMapping("/goods/list")
    public List<Map<String, Object>> listGoodsClassification(@RequestBody Map<String, Object> param, HttpServletRequest request) {
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        param.put("orgId", userinfo.getOrgId());
        return classificationService.listGoodsClassification(param);
    }
}
